package com.wcl.nullmusic.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.mode.Recommend_CategoryContents_List_List;
import com.wcl.nullmusic.ui.activity.AnthologyActivity;
import com.wcl.nullmusic.utils.NumberUtils;
import com.wcl.nullmusic.utils.ToActivityUtils;

import java.util.List;

/**
 * Created by dllo on 16/1/17.
 */
public class SongMenuRecyclerViewAdapter extends RecyclerView.Adapter<SongMenuRecyclerViewAdapter.ViewHolder> {
    private List<Recommend_CategoryContents_List_List> datas;
    private Context context;

    public SongMenuRecyclerViewAdapter(Context context) {
        this.context=context;
    }

    public void addData(List<Recommend_CategoryContents_List_List> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.songmenu_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(datas!=null&&datas.size()>0){
           final Recommend_CategoryContents_List_List contents_list_list= datas.get(position);
            holder.songmenu_item_layout_title.setText(contents_list_list.getTitle());
            holder.songmenu_item_layout_subTitle.setText(contents_list_list.getIntro());
            holder.songmenu_item_layout_counts.setText(NumberUtils.toUnit(contents_list_list.getPlaysCounts()));
            holder.songmenu_item_layout_tracksCounts.setText(contents_list_list.getTracksCounts()+"集");

            Picasso.with(context).load(contents_list_list.getCoverMiddle()).placeholder(R.mipmap.ic_classify_img07)
                    .error(R.mipmap.ic_classify_img07).into(holder.songmenu_item_layout_pic);

            holder.songmenu_item_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("album", contents_list_list.getAlbumId() + "");
                    bundle.putString("position", 1 + "");
                    bundle.putString("title", contents_list_list.getTitle());
                    ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return datas!=null&&datas.size()>0?datas.size():0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout songmenu_item_layout;
        private ImageView songmenu_item_layout_pic;
        private TextView songmenu_item_layout_title,songmenu_item_layout_subTitle,
                songmenu_item_layout_counts,songmenu_item_layout_tracksCounts;
        public ViewHolder(View itemView) {
            super(itemView);
            songmenu_item_layout= (RelativeLayout) itemView.findViewById(R.id.songmenu_item_layout);
            songmenu_item_layout_pic= (ImageView) itemView.findViewById(R.id.songmenu_item_layout_pic);
            songmenu_item_layout_title= (TextView) itemView.findViewById(R.id.songmenu_item_layout_title);
            songmenu_item_layout_subTitle= (TextView) itemView.findViewById(R.id.songmenu_item_layout_subTitle);
            songmenu_item_layout_counts= (TextView) itemView.findViewById(R.id.songmenu_item_layout_counts);
            songmenu_item_layout_tracksCounts= (TextView) itemView.findViewById(R.id.songmenu_item_layout_tracksCounts);

        }
    }
}
